package org.bambrikii.web.site.services.dictionary2;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.bambrikii.dictionary.Article;
import org.bambrikii.dictionary.Word;
import org.bambrikii.dictionary.managers.DictionaryManager;
import org.bambrikii.site.base.business.managers.core.ManagerException;
import org.bambrikii.site.base.model.Pager;
import org.bambrikii.site.base.struts2.core.SiteBaseBean;
import org.bambrikii.site.base.struts2.core.beans.ManagerContainerBean;

public class Dictionary2Bean extends SiteBaseBean {

	private DictionaryManager dictionaryManager;

	public Dictionary2Bean(DictionaryManager dictionaryManager) {
		this.dictionaryManager = dictionaryManager;
	}

	public Article saveArticle(final String url, final String title, final String body) throws ManagerException {
		final DictionaryManager mgr = (DictionaryManager) getContainer().getManager(DictionaryManager.class);
		try {
			getContainer().begin();
			final Article article = new Article();
			article.setSourceLocation(url);
			article.setTitle(title);
			article.setBody(body);
			mgr.saveOrUpdate(article);
			getContainer().commit();
			return article;
		} catch (final ManagerException ex) {
			getContainer().rollback();
			throw ex;
		}
	}

	public Pager<Article> getArticlesPager(final String title, final String order, final int currPage)
			throws ManagerException {
		return dictionaryManager.getArticlesPager(title, order, currPage);
	}

	public void saveWord(final Integer articleId, final String word, final String meaning) throws ManagerException {
		try {
			getContainer().begin();
			final Article article = dictionaryManager.getById(articleId);
			final Word w = dictionaryManager.saveWord(word);
			dictionaryManager.setWordContext(article, w, meaning);
			getContainer().commit();
		} catch (final ManagerException ex) {
			getContainer().rollback();
			throw ex;
		}
	}

	public Article getArticle(final Integer articleId) throws ManagerException {
		final DictionaryManager mgr = (DictionaryManager) getContainer().getManager(DictionaryManager.class);
		return dictionaryManager.getById(articleId);
	}

	public Article saveArticle(final Integer articleId, final String title, final String body,
			final String sourceLocation) throws ManagerException {
		try {
			getContainer().begin();
			final Article article = dictionaryManager.getById(articleId);
			article.setTitle(title);
			article.setBody(body);
			article.setSourceLocation(sourceLocation);
			getContainer().commit();
			return article;
		} catch (final ManagerException ex) {
			getContainer().rollback();
			throw ex;
		}
	}

	private List<String> getWords(final String body) {
		final String[] words1 = body.split("[\\s\\,\\.\\:]+");
		final List<String> words2 = new ArrayList<String>();
		for (final String word : words1) {
			words2.add(word);
		}
		return words2;
	}

	public Map<String, Map<Integer, String>> getContexts(final Article article) throws ManagerException {
		final List<String> words = getWords(article.getBody());
		final Map<String, Map<Integer, String>> words2 = dictionaryManager.getContexts(article, words);
		for (final String word : words) {
			if (!words2.containsKey(word)) {
				words2.put(word, null);
			}
		}
		return words2;
	}

	public void saveContexts2(final Integer articleId, final Map<String, String> words) throws ManagerException {
		getContainer().begin();
		try {
			final Article article = dictionaryManager.getById(articleId);
			for (final String word : words.keySet()) {
				final String context = words.get(word);
				final Word word2 = dictionaryManager.saveWord(word);
				dictionaryManager.setWordContext(article, word2, context);
			}
			getContainer().commit();
		} catch (final ManagerException ex) {
			getContainer().rollback();
			throw ex;
		}
	}
}
